﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using GamePlugin.Logics.Core;

namespace GamePlugin.Logics.Data
{
    public class PropertyTreeItem : DependencyObject
    {
        PropertyTreeItem _Parent;
        public PropertyTreeItem(PropertyTreeItem parent, string name)
        {
            _Parent = parent;
            this.Name = name;
            this.Children = new ObservableCollection<PropertyTreeItem>();
            if (parent != null)
                parent.Children.Add(this);
        }

        public string FullName
        {
            get
            {
                string value = this.Name;
                if (_Parent != null)
                    value = _Parent.FullName + this.Name;
                return value;
            }
        }
        public bool IsLeaf
        {
            get
            {
                return this.Children.Count == 0;
            }
        }

        public void TrySelectItem(string name)
        {
            this.IsExpanded = false;
            if (name.StartsWith(this.Name) == false)
                return;
            if (name.StartsWith(name))
            {
                this.IsExpanded = true;
                name = name.Remove(0, this.Name.Length);
                if (name.Length == 0)
                    this.IsSelected = true;
                else
                {
                    foreach (var child in this.Children)
                    {
                        child.TrySelectItem(name);
                    }
                }
            }
        }

        public bool RemoveLeaf(string name)
        {
            if (this.IsLeaf == false)
            {
                foreach (var item in this.Children)
                {
                    if (item.RemoveLeaf(name))
                        return true;
                }
            }
            else
            {
                if (this.Name == name)
                {
                    this._Parent.Children.Remove(this);
                    return true;
                }
            }
            return false;
        }

        public void BuildLeaf(string name)
        {
            if (name == null || name.Length == 0)
                return;

            bool found = false;
            foreach (var item in this.Children)
            {
                if (name.StartsWith(item.Name))
                {
                    found = true;
                    item.BuildLeaf(name.Remove(0, item.Name.Length));
                }
            }

            if (found == false)
            {
                new PropertyTreeItem(this, name);
            }
        }

        public static readonly DependencyProperty IsExpandedProperty =
        DependencyProperty.Register("IsExpanded", typeof(bool), typeof(PropertyTreeItem), null);
        [Editable]
        public bool IsExpanded
        {
            get { return (bool)GetValue(IsExpandedProperty); }
            set { SetValue(IsExpandedProperty, value); }
        }

        public static readonly DependencyProperty IsSelectedProperty =
        DependencyProperty.Register("IsSelected", typeof(bool), typeof(PropertyTreeItem), null);
        [Editable]
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(PropertyTreeItem), null);
        [Editable]
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        public static readonly DependencyProperty ChildrenProperty =
            DependencyProperty.Register("Children", typeof(ObservableCollection<PropertyTreeItem>), typeof(PropertyTreeItem), null);
        [Editable]
        public ObservableCollection<PropertyTreeItem> Children
        {
            get { return (ObservableCollection<PropertyTreeItem>)GetValue(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }
    }
}
